package com.ocean;

import java.security.MessageDigest;
import java.util.Date;
import java.util.Random;

import com.ocean.proxy.thrift.entity.AdRecomReq;
import com.ocean.proxy.thrift.entity.AdSpaceType;
import com.ocean.proxy.thrift.entity.AdUserInfo;
import com.ocean.proxy.thrift.entity.UserAdSpaceAttri;

public class AdReqBuilder {

//	private static final String APP_ID = "134";
//
//	private static final String SPACE_ID = "37779";

	private static final int WIDTH = 600;

	private static final int HEIGHT = 300;

	// private static final String USER_ID = "123456001";

	private static final String USER_ID = "869023020498185";
	
	private static final int AD_NUM = 1;

	public static AdRecomReq ordinary() {
		AdRecomReq adReq = new AdRecomReq();
		adReq.setHash("test_request_id");
		adReq.setResult_num(AD_NUM);
		adReq.setVersion("1.0.0");
		adReq.setApp("AlphaGo");
		adReq.setOgin_name("");
		adReq.setType("");
		adReq.setNet("wifi");
		adReq.setUserAdSpaceAttri(ordinaryUserAdSpaceAttri());
		adReq.setUserinfo(ordinaryUserInfo());
		return adReq;
	}

	private static AdUserInfo ordinaryUserInfo() {
		AdUserInfo info = new AdUserInfo();
		info.setImei(USER_ID);
		info.setOs("android");
		info.setOsversion("5.1");
		info.setBrand_name("ivvi");
		info.setPhonemodel("ivvi SS1-03");
//		info.setPhonemodel("OPPO A51w");
		info.setMobile("CMCC");
		info.setClient_ip(ordinaryIp());
		info.setCity("");
		info.setLon("25.123");
		info.setLat("54.231");
		info.setUa("Mozilla/5.0 (Linux; Android 5.1.1; ivvi SS1-03 Build/LMY47V; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043220 Safari/537.36");
		info.setAaid("d16b435a4da7e990");
//		info.setAdid("");
//		info.setIdfa("");
		info.setDevice_width(720);
		info.setDevice_height(1280);
		info.setMac("d03742827d71");
		info.setImsi("460021861117738");
//		info.setDip("");
//		info.setDensity("");
		return info;
	}

	private static UserAdSpaceAttri ordinaryUserAdSpaceAttri() {
		UserAdSpaceAttri attr = new UserAdSpaceAttri();
		attr.setSpaceWidth(WIDTH);
		attr.setSpaceHeight(HEIGHT);
		attr.setSpaceType(AdSpaceType.OPENING);
		return attr;
	}

	private static String ordinaryIp() {
//		return randomIp();
//		//深圳
		return "183.14.133.73";
//		//北京
//		net.setRemoteIp("123.125.71.38");
//		//上海
//		net.setRemoteIp("114.80.68.233");
//		//广州
//		net.setRemoteIp("219.137.148.2");
		
		// 郑州
//		return "218.28.234.181";
		// 佛山
//		net.setRemoteIp("221.4.148.7");
//		// 合肥
//		net.setRemoteIp("218.22.9.23");
//		// 呼和浩特
//		net.setRemoteIp("61.138.111.103");
//		// 西安
//		net.setRemoteIp("61.134.0.13");
//		// 苏州
//		net.setRemoteIp("218.4.57.245");
	}

//	public static AdRecomReq special(String requestId, String appId, String spaceId, int width, int height) {
//		AdRecomReq req = ordinary();
//		req.setRequestId(requestId);
//		req.getApp().setAppId(appId);
//		req.getAdspaces().get(0).setSpaceId(spaceId);
//		req.getAdspaces().get(0).setWidth(width);
//		req.getAdspaces().get(0).setHeight(height);
//		return req;
//	}

	@SuppressWarnings("unused")
	private static String randomMD5() {
		return md5Encode(String.valueOf(new Date().getTime() + new Random().nextInt(100)));
	}

	private static String randomIp() {
		Random random = new Random();
		return "183." + random.nextInt(256) + "." + random.nextInt(256) + "." + random.nextInt(256);
	}

	public static String md5Encode(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			byte[] byteArray = inStr.getBytes("UTF-8");
			byte[] md5Bytes = md5.digest(byteArray);
			StringBuffer hexValue = new StringBuffer();
			for (int i = 0; i < md5Bytes.length; i++) {
				int val = (md5Bytes[i]) & 0xff;
				if (val < 16) {
					hexValue.append("0");
				}
				hexValue.append(Integer.toHexString(val));
			}
			return hexValue.toString();
		} catch (Exception e) {
			return "";
		}
	}
}
